package com.example.studentmanagement.service.impl;

import com.example.studentmanagement.entity.User;
import com.example.studentmanagement.mapper.UserMapper;
import com.example.studentmanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 〈功能概述〉<br>
 *
 * @className: UserServiceImpl
 * @author: 程序员Mars
 * @date: 2024/12/25 23:45
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.findAll();
    }

    @Override
    public void createUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public User authenticateUser(String username, String password) {
        User user = userMapper.selectByUserName(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;  // 登录成功
        }
        return null;  // 登录失败
    }
}